<?php

namespace App\Store\Models\Goods;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\GoodsBrand;
use App\Store\Models\Basic\GoodsCategory;
use App\Store\Models\Basic\GoodsContent;
use App\Store\Models\Basic\GoodsCraft;
use App\Store\Models\Basic\GoodsPricetype;
use App\Store\Models\Basic\GoodsStone;
use App\Store\Models\Basic\GoodsStyle;
use App\Store\Models\Basic\PaytypeList;
use App\Store\Models\Basic\Supply;

class GoodsStockinInfolist extends Model
{
    protected $table = 'site_goods_stockin_infolist';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];
    /**
     *获取列表
     */
    public static function getList(Request $request)
    {

        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['stockinmain','storeinfo','goods_category','supply','goods_style','goods_craft','goods_pricetype','goods_stone','paytype_info','goods_brand','goods_content'])->where(['cp_id' => $cp_id])

        ->orderBy(GoodsStockin::select('create_at')->whereColumn('id', 'site_goods_stockin_infolist.p_id')->limit(1), 'desc')
        ->orderBy('stockin_at', 'desc');
        $store_ids = StoreCommon::getCurrentAccountStoreId($request);

        if($store_ids){//权限控制
            if(is_array($store_ids)){//获取当前权限下的门店
                $query->whereIn('store_id',$store_ids);
            }
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }


        $store_id = $request->store_id;//下拉单选搜索
        if ($store_id>0) {
            $query->where('store_id', $store_id);
        }
        //父数据
        $ids=$request->ids;
        $ids=is_array($ids)?$ids:(is_array(json_decode($ids,true))?json_decode($ids,true):[]);
        if(count($ids)>0){
            $query->whereIn('p_id',$ids);
        }

        //子数据
        $subids=$request->subids;
        $subids=is_array($subids)?$subids:(is_array(json_decode($subids,true))?json_decode($subids,true):[]);
        if(count($subids)>0){
            $query->whereIn('id',$subids);
        }



        //子数据
        $sub_goods_category_id=$request->sub_goods_category_id;
        $sub_goods_category_id=is_array($sub_goods_category_id)?$ids:(is_array(json_decode($sub_goods_category_id,true))?json_decode($sub_goods_category_id,true):[]);
        if(count($sub_goods_category_id)>0){
            $query->whereIn('goods_category_id',$sub_goods_category_id);
        }

        //子数据
        $sub_goods_style_id=$request->sub_goods_style_id;
        $sub_goods_style_id=is_array($sub_goods_style_id)?$ids:(is_array(json_decode($sub_goods_style_id,true))?json_decode($sub_goods_style_id,true):[]);
        if(count($sub_goods_style_id)>0){
            $query->whereIn('goods_style_id',$sub_goods_style_id);
        }


        $goods_sn_code=$request->goods_sn_code;
        if($goods_sn_code!=''){
            $query->where('goods_sn_code','like','%'.$goods_sn_code.'%');
        }

        $goods_sn_code_list=$request->goods_sn_code_list;
        $goods_sn_code_list=$goods_sn_code_list?(is_array($goods_sn_code_list)?$goods_sn_code_list:(explode(',',$goods_sn_code_list))):[];
        if(count($goods_sn_code_list)>0){
            $query->whereIn('goods_sn_code',$goods_sn_code_list);
        }

        $supply_id = intval($request->supply_id); //下拉单选搜索
        if ($supply_id > 0) {
            $query->where('supply_id', $supply_id);
        }

        $paymoney = intval($request->paymoney);//应付账款
        if ($paymoney > 0) {
            $query->where('paymoney', $paymoney);
        }
        $audit_status = intval($request->audit_status);//明细状态
        if ($audit_status > 0) {
            $query->where('audit_status', $audit_status);
        }
        $label_price= is_numeric($request->label_price)?$request->label_price:-1;
        if ($label_price >= 0) {
            $query->where('label_price_new', $label_price);
        }

        $piece_weight = $request->piece_weight;//重量
        if ($piece_weight > 0) {
            $query->where('piece_weight', $piece_weight);
        }



        $start_time = (!is_numeric($request->start_time) && strtotime($request->start_time) > 0) ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = (!is_numeric($request->end_time) && strtotime($request->end_time) > 0) ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > $start_time) {

            // $pids=GoodsStockin::where('procure_at', '>=', $start_time)->where('procure_at', '<=', $end_time)->pluck('id');
            $query->whereIn('p_id', function($query) use($start_time,$end_time) {
                // GoodsStockin::where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time)->pluck('id');
                $query->select('id')
                ->from(with(new GoodsStockin)->getTable())
                ->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
            });
        }


        $where=[];
        $order_sncode=$request->order_sncode;
        if($order_sncode!=''){
            $where[]=['order_sncode','like','%'.$order_sncode.'%'];
        }

        $goods_style_id=intval($request->goods_style_id);
        if($goods_style_id>0){
            $where[]=['goods_style_id',$goods_style_id];
        }

        $goods_category_id=intval($request->goods_category_id);
        if($goods_category_id>0){
            $where[]=['goods_category_id',$goods_category_id];
        }

        $audit_status=intval($request->audit_status);
        if($audit_status>0){
            $where[]=['audit_status',$audit_status];
        }

        $query->where($where);



        $isgetall = intval($request->isgetall) ?? 0;


        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function storeinfo(){
        return $this->hasOne(SiteStore::class, 'id', 'store_id')->select('id','store_name','store_file_path','tracing_url','qrcode_url','brand_url');
    }

    public function goods_category(){
        return $this->hasOne(GoodsCategory::class, 'id', 'goods_category_id')->select('id','category_name','is_gram','is_sujin');
    }

    public function supply(){
        return $this->hasOne(Supply::class, 'id', 'supply_id')->select('id','supply_name');
    }

    public function goods_style(){
        return $this->hasOne(GoodsStyle::class, 'id', 'goods_style_id')->select('id','style_name');
    }

    public function goods_craft(){
        return $this->hasOne(GoodsCraft::class, 'id', 'goods_craft_id')->select('id','craft_name');
    }
    public function goods_pricetype(){
        return $this->hasOne(GoodsPricetype::class, 'id', 'goods_pricetype_id')->select('id','pricetype_name');
    }
    public function goods_stone(){
        return $this->hasOne(GoodsStone::class, 'id', 'goods_stone_id')->select('id','stone_name');
    }
    public function paytype_info(){
        return $this->hasOne(PaytypeList::class, 'id', 'paytype_id')->select('id','paytype_name');
    }
    public function goods_brand(){
        return $this->hasOne(GoodsBrand::class, 'id', 'goods_brand_id')->select('id','brand_name');
    }

    public function stockinmain(){
        return $this->hasOne(GoodsStockin::class, 'id', 'p_id')->select('id','create_at');
    }

    public function goods_content(){
        return $this->hasOne(GoodsContent::class, 'id', 'goods_content_id')->select('id','content_name');
    }

}
